home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / Nuntius / NetAsciiTools.cp < prev    next >
Encoding:
Text File  |  1994-03-12  |  5.6 KB  |  244 lines  |  [TEXT/MPS ]

  1. // Copyright © 1992 Peter Speck, speck@dat.ruc.dk. All rights reserved.
  2. // NetAsciiTools.cp
  3.  
  4. #include "NetAsciiTools.h"
  5. #include "Tools.h"
  6. #include "UPrefsDatabase.h"
  7.  
  8. #include <ErrorGlobals.h>
  9.  
  10. #include <Resources.h>
  11. #include <Errors.h>
  12.  
  13. #pragma segment MyTools
  14. //--------------------------------------------------------------------------
  15. CharsetTranslateTableType gUS2Finnish, gUS2swe, gUS2dk, gToLowerChar, gMac2NetAsctii;
  16. CharsetTranslateTableType gNetAscii2Mac, gMac2NetAscii, gROT13Table;
  17.  
  18. void FillTable(CharsetTranslateTablePtr table, short first = 0, short last = 255, short offset = 0)
  19. {
  20.     for (short i = first; i <= last; i++)
  21.         table[i] = i + offset;
  22. }
  23.  
  24. void MakeTableExceptions(CharsetTranslateTablePtr table, const char *p)
  25. {
  26.     const unsigned char *up = (const unsigned char *)p;
  27.     while (*up)
  28.     {
  29.         unsigned char uch = *up++;
  30.         table[uch] = *up++;
  31.     }
  32. }
  33.  
  34. void TranslateCStr255(CStr255 &s, const CharsetTranslateTablePtr table)
  35. {
  36.     unsigned char *p = &s[1];
  37.     for (short i = s.Length(); i; --i)
  38.     {
  39.         *p = table[*p];
  40.         p++;
  41.     }
  42. }
  43.  
  44. void LoadTranslateTable(const CStr255 &rsrcName, CharsetTranslateTablePtr table1, CharsetTranslateTablePtr table2)
  45. {
  46.     Handle h = GetNamedResource('TCHR', rsrcName);
  47.     if (!h && (ResError() == noErr || ResError() == resNotFound))
  48.     {
  49.         ParamText(rsrcName, gEmptyString, gEmptyString, gEmptyString);
  50.         StdAlert(phMissingTranslateTable);
  51.         return;
  52.     }
  53.     FailNILResource(h);
  54.     BytesMove(*h, table1, 256);
  55.     BytesMove(*h + 256, table2, 256);
  56. }
  57.  
  58. void TranslateViaTable(const CharsetTranslateTablePtr table, char *textP, long len)
  59. {
  60.     unsigned char *p = (unsigned char*)textP;
  61.     while (len > 0)
  62.     {
  63.         *p = table[*p];
  64.         p++;
  65.         len--;
  66.     }
  67. }
  68. //--------------------------------------------------------------------------
  69. void InitUNetAsciiTools()
  70. {
  71.     FillTable(gUS2dk);                MakeTableExceptions(gUS2dk, "[Æ\\Ø]Å{æ|ø}å");
  72.     FillTable(gUS2swe);                MakeTableExceptions(gUS2swe, "[Ä\\Ö]Å{ä|ö}å");
  73.     FillTable(gUS2Finnish);        MakeTableExceptions(gUS2Finnish, "[Ä\\Ö]Å{ä|ö}å~ü^Ü`è");
  74.     FillTable(gToLowerChar);    FillTable(gToLowerChar, 'A', 'Z', 'a' - 'A');
  75.     FillTable(gMac2NetAsctii);
  76.     FillTable(gROT13Table);
  77.     FillTable(gROT13Table, 'A', 'M',  13); FillTable(gROT13Table, 'a', 'm',  13);
  78.     FillTable(gROT13Table, 'N', 'Z', -13); FillTable(gROT13Table, 'n', 'z', -13);
  79.     for (short i = 128; i <= 255; i++)
  80.         gMac2NetAsctii[i] = '!';
  81.     CStr255 s;
  82.     gPrefs->GetStringPrefs('Tran', s);
  83.     LoadTranslateTable(s, gMac2NetAscii, gNetAscii2Mac);
  84. }
  85. //--------------------------------------------------------------------------
  86.  
  87. void StripSpaces(CStr255 &s)
  88. {
  89.     short i = s.Length();
  90.     while (i && s[i] <= 32)
  91.         --i;
  92.     s.Length() = i;
  93.     while (i && s[1] <= 32)
  94.     {
  95.         --i;
  96.         s.Delete(1, 1);
  97.     }
  98. }
  99.  
  100. //--------------------------------------------------------------------------
  101. void MakeLastCharCR(Handle h)
  102. {
  103.     HUnlock(h); // no restore!
  104.     long size = GetHandleSize(h);
  105.     char lastChar = *Ptr(*h + size - 1);
  106.     if (lastChar == 13) 
  107.         return;
  108.     else if (lastChar == 10) {
  109.         if (*Ptr(*h + size - 2) != 13)
  110.             *Ptr(*h + size - 1) = 13; // replace LF by CR
  111.         else {
  112.             SetPermHandleSize(h, --size); // strip LF off
  113.         }            
  114.     }
  115.     else { // Add cr
  116.         SetPermHandleSize(h, ++size);
  117.         *Ptr(*h + size - 1) = 13;
  118.     }
  119. }
  120.  
  121. void MakeLastCharNull(Handle h)
  122. {
  123.     HUnlock(h); // no restore!
  124.     long size = GetHandleSize(h);
  125.     char lastChar = *(*h + size - 1);
  126.     if (lastChar == 0) 
  127.         return;
  128.     else
  129.     {
  130.         // Add null
  131.         SetPermHandleSize(h, ++size);
  132.         *(*h + size - 1) = 0;
  133.     }
  134. }
  135.  
  136.  
  137. //--------------------------------------------------------------------------
  138. void StoreAuthorText(CStr255 &s, CStr255 &realName, CStr255 &email, Boolean &isEmail)
  139. {
  140.     if (s.Length())
  141.     {
  142.         if (isEmail)
  143.         {
  144.             if (!email.Length())
  145.                 email = s;
  146.         }
  147.         else
  148.         {
  149.             if (!realName.Length())
  150.                 realName = s;
  151.         }
  152.         s = "";
  153.     }
  154.     isEmail = false;
  155. }
  156.  
  157. void GetAuthorName(const CStr255 &text, CStr255 &realName, CStr255 &email)
  158. {
  159.     realName = "";
  160.     email = "";
  161.     CStr255 s("");
  162.     short index = 1;
  163.     Boolean isEmail = false;
  164.     while (index <= text.Length())
  165.     {
  166.         switch (text[index])
  167.         {
  168.             case '"':
  169.             case '>':
  170.             case ')':
  171.             case ',':
  172.                 StoreAuthorText(s, realName, email, isEmail);
  173.                 break;
  174.  
  175.             case '(':
  176.                 StoreAuthorText(s, realName, email, isEmail);
  177.                 break;
  178.             
  179.             case '<':
  180.                 StoreAuthorText(s, realName, email, isEmail);
  181.                 isEmail = true;
  182.                 s = "";
  183.                 break;
  184.                 
  185.             case '@':
  186.                 isEmail = true;
  187.                 s += text[index];
  188.                 break;
  189.                 
  190.             default:
  191.                 if (text[index] != 32 || s.Length() > 0)
  192.                     s += text[index];
  193.         }
  194.         ++index;
  195.     }
  196.     StoreAuthorText(s, realName, email, isEmail);
  197.     short i = realName.Length();
  198.     while (i && realName[i] == 32)
  199.         --i;
  200.     realName.Length() = i;
  201.     i = email.Length();
  202.     while (i && email[i] == 32)
  203.         --i;
  204.     email.Length() = i;
  205. }
  206.  
  207. void GetNationalAuthorNames(const CStr255 &text, CStr255 &realName, CStr255 &email)
  208. {
  209.     GetAuthorName(text, realName, email);
  210.     if (email.Length() > 3)
  211.     {        
  212.         unsigned char *p = &email[email.Length() - 2];
  213.         unsigned char uch, uch2;
  214.         if (*p == '.')
  215.         {
  216.             ++p;
  217.             uch = gToLowerChar[*p];
  218.             uch2 = gToLowerChar[*++p];
  219.             if (     uch == 'd' && uch2 == 'k')
  220.                 TranslateCStr255(realName, gUS2dk);
  221.             else if (uch == 's' && uch2 == 'e')
  222.                 TranslateCStr255(realName, gUS2swe);
  223.             else if (uch == 'n' && uch2 == 'o')
  224.                 TranslateCStr255(realName, gUS2dk);
  225.             else if (uch == 'f' && uch2 == 'i')
  226.                 TranslateCStr255(realName, gUS2Finnish);
  227.         }
  228.     }
  229. }
  230.  
  231. Boolean GetPrintableAuthorName(const CStr255 &text, CStr255 &name, CStr255 &email)
  232. {
  233.     GetNationalAuthorNames(text, name, email);
  234.     if (name.Length())
  235.         return true;
  236.     name = email;
  237.     short atPos = name.Pos("@"); // use email instead, but remove the site name
  238.     if (atPos > 1)
  239.         name.Length() = atPos - 1;
  240.     return false;
  241. }
  242. //--------------------------------------------------------------------------
  243.  
  244.